<?php
namespace app\api\service;
use think\facade\{View,Session,Cache,Db};
use think\exception\HttpResponseException;
require_once '../extend/alipay/wappay/service/AlipayTradeService.php';
require_once '../extend/alipay/wappay/buildermodel/AlipayTradeRefundContentBuilder.php';
class Alipay
{
    public function pay($orderId,$price){
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2021004136662427';
        $aop->rsaPrivateKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC+qAMlLTO4k1AMHP2LJ58+7hz3cV7P3c70hGaVNDcPbLCQJYIXyfwjWon6byDUe9h8guk8jH2wDfnJcV2mickTjFy0pVjmiRQOPAmy12s5Qch6x9TEPo8kkAbjlUqA3TEOGvxUlRQYLC/6gRuV4Un1d7Ar32WKpt5MCJzwfegn+/RBhG1cfTE0Vz4ObEasgMBn14Al4eG6yF5/4jISNV6srfwO1t6BzAclEYvYljh1wXdLHfBclI+iZtZrACFxNRYSDK1oqB2jhZnAOiQ3fb0sMmw08WyQQLbLIBD5zxLs4h+pPWqjVbtrGooHwB6hsQIUr7lFRzTMWxlj/zQrbgtlAgMBAAECggEAVNGiNJPbgq3m2ry6/4FtEy4TrPG+4HMpcxbEHHTSWgZctoQ5UCQIDCJnaAawcFd4hEwpP0qxT/Wnvrq0kzaRQiUbyTIWTtmFFqAXIH09sodVjWoBehzqybVZmz37m0DMZh4TS/3WprphzexKPW+w24eX1KzaG8kI9IKQb11UPxTPQblBqTlX7sAThCIqWF5C/kYct7KJ8ftIfxmKz02VWwUnHgVSlksdsEXdloMycAjrJGK6g99FenwRGIW4bBmSpS6y1uiGOri9TjFGRL1y7XEYUWCiqC5J/DgRYrf6hkUJ5nFo8SUQeSz2WUJfV0PPwwQ+AdVXgvZlbaViegL6AQKBgQDpgzGDDAi1QYul2srCQuABKOJ2s/U2VHNAauOrD0Tf6c7e8BJoKzMSDk3QYATt4/CnGLpxw6VOubzauxybQzfbDIVGQ9tomv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HVJhHUegh2ySAjn9INyB6KGyHDECWcuGRJBzU35IzjfdRpozyvBxVcZXmCTgw2ibCw2IXs18qekPBgFem/kDRHzkemFrgRSsx0LXysxWfZE8096lQIDAQAB';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        /******必传参数******/
        $object = new \stdClass();
        //商户订单号，商家自定义，保持唯一性
        //$orderId;
        $object->out_trade_no = time().uniqid();
         if($_SERVER['REMOTE_ADDR']=="123.174.45.200"){
             $price=0.01;
         }
        Db::name("wxpay_log")->insert([
            "aut_id"=>$orderId,
            "ut_type"=>1,
            "price"=>$price,
            "no"=>$object->out_trade_no,
            "addtime"=>time()
            ]);
        
        //支付金额，最小值0.01元
        $object->total_amount = $price;
        //订单标题，不可使用特殊符号
        $object->subject = '征信服务';
        /******可选参数******/
        //手机网站支付默认传值FAST_INSTANT_TRADE_PAY
        $object->product_code ='QUICK_WAP_WAY';
        $object->time_expire = date("Y-m-d H:i:s",time()+2*60*60);
        $json = json_encode($object);
        $request = new \AlipayTradeWapPayRequest();
        //异步接收地址，仅支持http/https，公网可访问
        $request->setNotifyUrl(_doadmin."api/returnpay/returnAliROrder");
        //同步跳转地址，仅支持http/https
        $request->setReturnUrl(_doadmin."h5/#/pages/index/index?id=".$orderId."&admin=http://".input('admin'));
        $request->setBizContent($json);
        $result = $aop->pageExecute ( $request); 

        echo $result;
        return;
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
        echo "成功";
        } else {
        echo "失败";
        }
        
    }
    //退款
    public function repay($trade_no,$refund_amount,$refund_reason="同意退款"){
      
        $RequestBuilder=new \AlipayTradeRefundContentBuilder();
    $RequestBuilder->setOutTradeNo($trade_no);
        //$RequestBuilder->setTradeNo($trade_no);
        $RequestBuilder->setRefundAmount($refund_amount);
//        $RequestBuilder->setOutRequestNo($out_request_no);
        $RequestBuilder->setRefundReason($refund_reason);
        $config=[
             "gatewayUrl" => 'https://openapi.alipay.com/gateway.do',
        "app_id" => '2021004136662427',
        "merchant_private_key" => 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC+qAMlLTO4k1AMHP2LJ58+7hz3cV7P3c70hGaVNDcPbLCQJYIXyfwjWon6byDUe9h8guk8jH2wDfnJcV2mickTjFy0pVjmiRQOPAmy12s5Qch6x9TEPo8kkAbjlUqA3TEOGvxUlRQYLC/6gRuV4Un1d7Ar32WKpt5MCJzwfegn+/RBhG1cfTE0Vz4ObEasgMBn14Al4eG6yF5/4jISNV6srfwO1t6BzAclEYvYljh1wXdLHfBclI+iZtZrACFxNRYSDK1oqB2jhZnAOiQ3fb0sMmw08WyQQLbLIBD5zxLs4h+pPWqjVbtrGooHwB6hsQIUr7lFRzTMWxlj/zQrbgtlAgMBAAECggEAVNGiNJPbgq3m2ry6/4FtEy4TrPG+4HMpcxbEHHTSWgZctoQ5UCQIDCJnaAawcFd4hEwpP0qxT/Wnvrq0kzaRQiUbyTIWTtmFFqAXIH09sodVjWoBehzqybVZmz37m0DMZh4TS/3WprphzexKPW+w24eX1KzaG8kI9IKQb11UPxTPQblBqTlX7sAThCIqWF5C/kYct7KJ8ftIfxmKz02VWwUnHgVSlksdsEXdloMycAjrJGK6g99FenwRGIW4bBmSpS6y1uiGOri9TjFGRL1y7XEYUWCiqC5J/DgRYrf6hkUJ5nFo8SUQeSz2WUJfV0PPwwQ+AdVXgvZlbaViegL6AQKBgQDpgzGDDAi1QYul2srCQuABKOJ2s/U2VHNAauOrD0Tf6c7e8BJoKzMSDk3QYATt4/CnGLpxw6VOubzauxybQzfbDIVGQ9tomvohfwi22Z2g5c6lbWvwMGL/rX9rr4LTPdyjVTyLytC2GH782fTRMJ8NDT0JYoMyIebtiJab96L+RQKBgQDRBEf/eICOR+31YIBXQmRYBPZGZmBGWdGH6sKp6izl8VCteflQjQ5MVfyu0u2U/6VgE+wFZ9qyFvm/YOA/4r3Y8DvJS0NfV/+dtBZzLd3vkzw23X/CwCkjF5GQgk31c8+ApxJUy9K683N7bE3O9IB+J0SZSNWPLxczlVoNnla6oQKBgQCgLXEaHngetW5wf4uUA8GaqgvmNv1enkFMj8XKAjhRjBVukalesTtgCFXSOPtIn5HlXS4PeY/8V6Y3OCCgVFSNOqJ1F3j64UvxDC3TF6VryBefHFbtNJPEJKQCHyuTcZzWoVJd5eqS7my1T2EC2eJQVqNmu5+ZFO5OQ+Jj7FNvLQKBgBfo/nd6CHK+RblhLa1Nuf7bqnSJN5A0rCR5NYWMdVBkl48QufV9/Vm8KGizVlxKFlf0T3aa18ixmiZLZbvJK2CKsrwnMPDSNzqKJFtOrum+FRBNrFTR+KK8CnnWfiaK9fzIn+81VcjvGpHO5OO80oob4dwLGesEPwo2lpOiPlehAoGBAIg2ZQiw1GIyudJQzQJ791Wk9sH5Cee8e0Hq0E6W0XHb+W3gzgH2wTIj8KT1LqHsjRGjze6qxCZvCbDCFSUONKJwzz0LbEKMegeQ7PVa5R8ts6WR0Mz+Uq2dF4vW4tErNHj/mWyl4AqAcuCz0ouv1j3rGC1GfL+huqMrtFDk0Kz0',
        'alipay_public_key'=>'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk8P05gYc3LJXCJa41HzudjyOhfPJTs+F/QZKYIytswjeisea9MqvbS17KDHganE1TqQDuel0wVT23sGYD2CHfo1PuqRly38CZoCCSeVQl8jn/+vzdkx0gzSqqkzt+2MPgwXpXMVZmfmYVOH91CYcDPt3o2i7PFZGtOzpTm/3cAt6OCzA7BMMwlD7X9S+JH7TErp2Sr+XTpFASpROz+Xtt03cmskUQaR6CTVkDHVJhHUegh2ySAjn9INyB6KGyHDECWcuGRJBzU35IzjfdRpozyvBxVcZXmCTgw2ibCw2IXs18qekPBgFem/kDRHzkemFrgRSsx0LXysxWfZE8096lQIDAQAB',
       
        "sign_type" => 'RSA2',
        "charset"=>'UTF-8',
        "format"=>'json'
            
            
            ];
        $aop = new \AlipayTradeService($config);

        /**
         * alipay.trade.refund (统一收单交易退款接口)
         * @param $builder 业务参数，使用buildmodel中的对象生成。
         * @return $response 支付宝返回的信息
         */
        $response = $aop->Refund($RequestBuilder);

                
        return $response;
        
        
        
        
        
        
        
        
        
        
        
    }
    
    
}

